package cn.xshi.sys.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import cn.xshi.common.base.*;
import cn.xshi.sys.form.SysDepartInfoForm;
import cn.xshi.sys.model.SysDepartInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.JsonUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysDepartInfoService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
/**
 * @Desc 部门信息
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysDepartInfo")
@Api(value = "部门信息API",tags = "部门信息API",description = "部门信息API")
public class SysDepartInfoController extends BaseAction {

	@Resource
	private SysDepartInfoService sysDepartinfoService;

	/**
	 * 查询部门信息列表并分页
	 * @param sysDepartInfoForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询部门信息列表并分页", notes="查询部门信息列表并分页")
	@GetMapping(value="/list")
	@Auth(value = "/sysDepartInfo/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<SysDepartInfo>> getSysDepartInfoListByCondition(SysDepartInfoForm sysDepartInfoForm,Integer pageNo, Integer pageSize){
		commonHPagerByPageNo(pageNo,pageSize);
		List<SysDepartInfo> sysDepartInfoList = sysDepartinfoService.getSysDepartInfoListByCondition(sysDepartInfoForm);
		PageInfo<SysDepartInfo> page = new PageInfo<SysDepartInfo>(sysDepartInfoList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	* 查询单个部门信息
	* @param id
	*/
	@ApiOperation(value="查询单个部门信息", notes="查询单个部门信息")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysDepartInfo/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<SysDepartInfo> getSysDepartInfoById(@PathVariable("id")String id){

		SysDepartInfo sysDepartInfo = sysDepartinfoService.getSysDepartInfoById(id);

		if(!StringUtil.isEmpty(sysDepartInfo.getCreateId())){

			OauthAccountEntity createBy = getAccount(sysDepartInfo.getCreateId());

			if(null != createBy){
				sysDepartInfo.setCreateBy(createBy.getName());
			}

		}
		if(!StringUtil.isEmpty(sysDepartInfo.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysDepartInfo.getUpdateId());
			if(null != modifiedBy){
				sysDepartInfo.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysDepartInfo);
	}

	/**
	 *一级目录
	 * @return
	 */
	@ApiOperation(value="一级目录", notes="一级目录")
	@GetMapping(value="/one/level/lists")
	@Auth(value = "/sysDepartInfo/one/level/lists",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<SysDepartInfo>> lists(){
		List<SysDepartInfo> sysDepartInfoList = sysDepartinfoService.getSysDepartInfoList();
		return outDataStr(sysDepartInfoList);
	}

	/**
	 * 子级目录
	 * @param id
	 * @return
	 */
	@ApiOperation(value="子级目录", notes="子级目录")
	@GetMapping(value="/child/lists/{id}")
	@Auth(value = "/sysDepartInfo/child/lists",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<SysDepartInfo>> childLists(@PathVariable("id")String id){
		Map<String,Object> condition = new HashMap<>();
		condition.put("parentId",id);
		List<SysDepartInfo> sysDepartInfoList = sysDepartinfoService.getSysDepartInfoListChild(condition);
		return outDataStr(sysDepartInfoList);
	}

	/**
	* 添加
	* @param sysDepartInfo
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个部门信息", notes="创建单个部门信息")
	public BaseResult addSysDepartInfo(@RequestBody SysDepartInfo sysDepartInfo){
		int i = 0;
		if(null != sysDepartInfo){
			sysDepartInfo.setId(toUUID());
			if(StringUtil.isEmpty(sysDepartInfo.getParentId())){
				sysDepartInfo.setParentId("0");
			}
			i= sysDepartinfoService.addSysDepartInfo(sysDepartInfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param sysDepartInfo
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个部门信息", notes="编辑单个部门信息")
	public BaseResult updateSysDepartInfo(@RequestBody SysDepartInfo sysDepartInfo){
		int i = 0;
		if(null != sysDepartInfo){
			if(StringUtil.isEmpty(sysDepartInfo.getParentId())){
				sysDepartInfo.setParentId("0");
			}
			i= sysDepartinfoService.updateSysDepartInfo(sysDepartInfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除部门信息", notes="删除部门信息")
	public BaseResult delSysDepartInfo(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysDepartinfoService.delSysDepartInfo(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 读取部门树
	 * @param id
	 */
	@ApiOperation(value="根据Id查询部门树", notes="根据Id查询部门树")
	@GetMapping(value="/tree/{id}")
	@Auth(value = "/sysDepartInfo/tree",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult getSysDepartInfoTree(@PathVariable("id")String id){
		Map<String, Object> condition = new HashMap<String, Object>();
		List<BaseTreeGridEntity> list = new ArrayList<BaseTreeGridEntity>();
		List<SysDepartInfo> sysDepartInfoList = sysDepartinfoService.getSysDepartInfoListAll(condition);
		for(int i = 0; i < sysDepartInfoList.size(); i++){
			SysDepartInfo departinfo = sysDepartInfoList.get(i);
			BaseTreeGridEntity BaseTreeGridEntity = new BaseTreeGridEntity();
			BaseTreeGridEntity.setId(departinfo.getId());
			BaseTreeGridEntity.setPid(departinfo.getParentId());
			BaseTreeGridEntity.setText(departinfo.getName());
			BaseTreeGridEntity.setExpanded(true);
			BaseTreeGridEntity.setSingleClickExpand(true);
			BaseTreeGridEntity.setIcon("/deng/images/icons/target.png");
			list.add(BaseTreeGridEntity);
		}

		BaseTreeGridEntity baseTreeGridEntity = new BaseTreeGridEntity();
		List<BaseTreeGridEntity> baseTreeGridEntityList = baseTreeGridEntity.buildTree(list,"0");
		String json = JsonUtil.toFastJson(baseTreeGridEntityList);
		return outStr(json);
	}
	
	/**
	 * 根据各种情况查找集合不分页（流程设计器中处理组 发起组等使用）
	 * @param id
	 * @return
	 */
	@ApiOperation(value=" 根据各种情况查找集合不分页", notes=" 根据各种情况查找集合不分页")
	@GetMapping(value="/list/{id}")
	@Auth(value = "/sysDepartInfo/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<SysDepartInfo>> querySysDepartInfoList(@PathVariable("id")String id){
		List<SysDepartInfo> list = new ArrayList<SysDepartInfo>();
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id", id.split(","));
			list = sysDepartinfoService.querySysDepartInfoList(condition);
		}
		return  outItemsStr(list);
	}

	/**
	 * 读取部门树（Bootstrap---ztree）
	 * @param request
	 */
	@ApiOperation(value=" 查询部门树（Btree）", notes="查询部门树（Btree）")
	@GetMapping(value="/bztree")
	@Auth(value = "/sysDepartInfo/bztree",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult getSysDepartInfoBZTree(HttpServletRequest request){
		Map<String, Object> condition = new HashMap<String, Object>();
		List<BaseZTreeEntity> list = new ArrayList<BaseZTreeEntity>();
		List<SysDepartInfo> sysDepartInfoList = sysDepartinfoService.getSysDepartInfoListAll(condition);
		for(int i = 0; i < sysDepartInfoList.size(); i++){
			SysDepartInfo sysDepartInfo = sysDepartInfoList.get(i);
			BaseZTreeEntity BaseZTreeEntity = new BaseZTreeEntity();
			BaseZTreeEntity.setId(sysDepartInfo.getId());
			BaseZTreeEntity.setPId(sysDepartInfo.getParentId());
			BaseZTreeEntity.setText(sysDepartInfo.getName());
			BaseZTreeEntity.setName(sysDepartInfo.getName());
			BaseZTreeEntity.setExpanded(true);
			BaseZTreeEntity.setSingleClickExpand(true);
			list.add(BaseZTreeEntity);
		}
		BaseZTreeEntity baseZTreeEntity = new BaseZTreeEntity();
		List<BaseZTreeEntity> baseZTreeEntityList = baseZTreeEntity.buildTree(list,"0");
		String json = JsonUtil.toFastJson(baseZTreeEntityList);
		return outStr(json);
	}
}
